<template>
	<view>
		<goods-list :goods="goods" @goodsItemClick="goGoodsDetail"></goods-list>
		<view class="isOver" v-if="flag">----------我是有底线的---------</view>
	</view>
</template>

<script>
	import goodsList from '../../components/goods-list/goods-list.vue'
	export default {
		data() {
			return {
				pageindex: 1,
				goods:[],
				flag: false
			}
		},
		components:{'goods-list': goodsList},
		methods: {
			// 获取商品列表数据
			async getGoodsList(callback){
				const res = await this.$myRuquest({
					url: `/api/getgoods?pageindex=` + this.pageindex
				})
				this.goods = [...this.goods, ...res.data.message]
				callback && callback()
			},// 导航到商品详情页
			goGoodsDetail (id) {
				uni.navigateTo({
					url: '/pages/goods-detail/goods-detail?id='+id
				})
			}
		},
		onLoad() {
			this.getGoodsList()
		},
		onReachBottom() {
			console.log(this.goods.length )
			// 因为每个page有10条数据,你请求一个他就+10,所以如果长度小于他.那么说明我们的数据被请求完了
			if(this.goods.length < this.pageindex*10) return this.flag = true
			console.log('触底了')
			this.pageindex ++
			this.getGoodsList()
		},
		// 监听下拉
		onPullDownRefresh() {
			console.log('xiala')
			this.goods = []
			this.flag = false
			setTimeout(() => {
				this.getGoodsList(() => {
					uni.stopPullDownRefresh()
				})
			}, 1000)
		}
	}
</script>

<style lang="scss">
	.goods_list{
		background-color: #EEE;
	}
	.isOver {
		width: 100%;
		height: 50px;
		line-height: 50px;
		text-align: center;
		// background: #fff;
		font-size: 28rpx;
	}
</style>
